package template

import (
	"cdam/models"
	"cdam/util"
	"os"
	"strings"
	"text/template"
)

type ToolTip struct {
}



func InitTemplateEngine() (*ToolTip) {

	e := new(ToolTip).init()
	// engine.Init(parse)
	return e
}

func (e *ToolTip) init() *ToolTip {
	return e
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}


type Field struct {
	Name    string
	Type    string
	NotNull bool
	Comment string
	GoName  string
	IsPk    bool
	Width   int
}

type ParseModelDetail struct {
	FileName    string
	Fields      []Field
	TableName   string
	TypeName    string
	DialogWidth string
}


func (e *ToolTip) ParseModelDetail(details []models.TableDetail)(ParseModelDetail)  {
	fields := []Field{}
	for _,v := range details{
		field := Field{}
		field.Name = v.Name
		field.Type = e.ParseType(v.Type)
		field.GoName = util.SetFirstToUpper(v.Name)
		field.Comment = v.Comment
		field.NotNull = v.Notnull == "f"
		field.IsPk = len(v.Conname) > 0
		fields = append(fields,field)
	}
	modelDetail := ParseModelDetail{}
	modelDetail.Fields = fields
	modelDetail.FileName = "Hello World"
	if len(fields) > 10 {
		modelDetail.DialogWidth = "60%"
	} else {
		modelDetail.DialogWidth = "40%"
	}
	return modelDetail
}

func (e *ToolTip) ParseType(strType string) string {
	if strType == "bigint" {
		return "int64"
	} else if strings.Contains(strings.ToLower(strType), "character varying") {
		return "string"
	} else if strings.Contains(strings.ToLower(strType), "date") {
		return "time.Time"
	} else if strings.Contains(strings.ToLower(strType), "smallint") {
		return "int"
	} else {
		return strType
	}
}

func (e *ToolTip) ParseToCode(details []models.TableDetail, tablename string) ([]string, error) {
	// 设置model路径
	_dir := "./code/model"
	r, _ := PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	name := strings.Split(tablename, "_")[len(strings.Split(tablename, "_"))-1]
	modefile := _dir + "/" + name + ".go"
	file, err := os.OpenFile(modefile, os.O_CREATE|os.O_WRONLY, 0755)
	// 获取模板
	tmpl, err := template.ParseFiles("template/files/model.go.tpl")
	// 获取数据
	data := e.ParseModelDetail(details)
	data.TableName = tablename
	data.FileName = name
	data.TypeName = util.SetFirstToUpper(name)

	err = tmpl.Execute(file, data)

	// 设置controller 路径
	_dir = "./code/controller"

	r, _ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	controller := _dir + "/" + name + ".go"
	file, err = os.OpenFile(controller, os.O_CREATE|os.O_WRONLY, 0755)
	tmpl, err = template.ParseFiles("template/files/controller.go.tpl")
	err = tmpl.Execute(file, data)
	// 设置api
	_dir = "./code/vue/api"
	r, _ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	api := _dir + "/" + name + ".js"
	file, err = os.OpenFile(api, os.O_CREATE|os.O_WRONLY, 0755)
	tmpl, err = template.ParseFiles("template/files/api.js.tpl")
	err = tmpl.Execute(file, data)
	// 设置vue
	_dir = "./code/vue/" + name
	r, _ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	index := _dir + "/index.vue"
	file, err = os.OpenFile(index, os.O_CREATE|os.O_WRONLY, 0755)
	tmpl, err = template.ParseFiles("template/files/index.vue.tpl")
	err = tmpl.Execute(file, data)
	// 设置 create or update vue
	_dir = "./code/vue/" + name + "/components"
	r, _ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir, os.ModePerm)
	}
	create := _dir + "/createOrUpdate.vue"
	file, err = os.OpenFile(create, os.O_CREATE|os.O_WRONLY, 0755)
	if len(data.Fields) > 10 {
		tmpl, err = template.ParseFiles("template/files/createorupdateplus.vue.tpl")
	} else {
		tmpl, err = template.ParseFiles("template/files/createorupdate.vue.tpl")
	}
	err = tmpl.Execute(file, data)

	r,_ = PathExists(_dir)
	if r == false {
		os.MkdirAll(_dir,os.ModePerm)
	}


	if err != nil {
		panic(err)
	}
	result := []string{}

	result = append(result,modefile)
	return  result,err
}
